import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import org.jnativehook.keyboard.NativeKeyEvent;
import xyz.bmj.hotkey.HotKey;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class tt {
    public static void main(String[] args) {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//        clipboard.addFlavorListener(ev -> {});


        HotKey hk = HotKey.getInstance();
        hk.setKey(() -> {
//            ev.getSource()
//            System.out.println((sun.awt.windows.WClipboar)ev.getSource());

//            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//            clipboard.addFlavorListener(e -> {
//                System.out.println(e.getSource());
//            });
            // 获取剪贴板中的内容
            Transferable trans2 = clipboard.getContents(null);
            String sourceTime = null;

            if (trans2 != null) {
                // 判断剪贴板中的内容是否支持文本
                if (trans2.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    try {
                        // 获取剪贴板中的文本内容
                        sourceTime = (String) trans2.getTransferData(DataFlavor.stringFlavor);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println(sourceTime);
            DateTime sourceDate = DateUtil.parse(sourceTime);
            DateTime outDate = DateUtil.offsetDay(sourceDate, -1);
            outDate = DateUtil.offsetMonth(outDate, 7);
            List<String> gf = new ArrayList<>();
            for (char c : sourceTime.toCharArray()) {
                if (!NumberUtil.isNumber("" + c)) {
                    gf.add(String.valueOf(c));
//                    System.out.print(gf);
                }
            }
            String pt = "yyyy-MM-dd";
            switch (gf.size()) {
                case 2:
                    pt = String.format("yyyy%sMM%sdd", gf.toArray());
                    break;
                case 3:
                    pt = String.format("yyyy%sMM%sdd%s", gf.toArray());
                    break;
                case 0:
                    pt = "yyyyMMdd";
                    break;
            }
//            String pt = StringFormatter.format(gf.size() == 2 ? "yyyy%sMM%sdd" : "yyyy%sMM%sdd%s", gf.toArray()).getValue();
            String format = DateUtil.format(outDate, pt);
            // 封装文本内容
            Transferable trans = new StringSelection(format);
            System.out.println(format);
            // 把文本内容设置到系统剪贴板
            clipboard.setContents(trans, null);
            try {
                Robot robot = new Robot();
                robot.keyPress(KeyEvent.VK_CONTROL);
                robot.keyPress(KeyEvent.VK_V);
                robot.keyRelease(KeyEvent.VK_V);
                robot.keyRelease(KeyEvent.VK_CONTROL);
            } catch (AWTException e1) {
                e1.printStackTrace();
            }

        }, NativeKeyEvent.VC_C, NativeKeyEvent.CTRL_MASK);

    }
}
